package com.itheima.knowledge.manager.controller;

import com.itheima.knowledge.manager.domain.Catalog;
import com.itheima.knowledge.manager.domain.Knowledge;
import com.itheima.knowledge.manager.domain.NewTable;
import com.itheima.knowledge.manager.domain.english;
import com.itheima.knowledge.manager.service.KnowledgeService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Scanner;

public class KnowledgeController {
    private String flag;

    public KnowledgeController() {
    }

    public KnowledgeController(String flag) {
        this.flag = flag;
    }

    Scanner sc = new Scanner(System.in);
    KnowledgeService ks = new KnowledgeService();
    public void deleteCatalog() {
        System.out.println("请输入您要删除的知识选项:");
        String id;
        while (true) {
            id = sc.nextLine();
            if (id.equals("c")||id.equals("u")||id.equals("d")||id.equals("e")||id.equals("n")){
                System.out.println("无权删除系统选项!");
                return;
            }
            boolean exists = ks.existsCatalog(id);
            if (exists){
                ks.deleteCatalog(id);
                System.out.println("删除成功!");
                break;
            }else{
                System.out.println("您输入的选项不存在");
                break;
            }
        }

    }

    public void updataCatalog() {
        System.out.println("请输入您要修改的知识选项:");
        String id;
        while (true) {
            id = sc.nextLine();
            boolean exists = ks.existsCatalog(id);
            if (exists){
                break;
            }else{
                System.out.println("您输入的选项不存在,请重新输入:");
            }
        }
        System.out.println("请输入您要更新后的知识点:");
        String knowledge = sc.nextLine();
        ks.updataCatalog(id,knowledge);
        System.out.println("修改成功!");
    }

    public void addCatalog() {
        String id;
        System.out.println("请输入您要添加的知识选项:");
        while (true) {
            id = sc.nextLine();
            boolean exists = ks.existsCatalog(id);
            if (!exists) {
                break;
            } else {
                System.out.println("你输入的知识选项已经存在,请重新输入:");
            }
        }
        System.out.println("请输入您要添加的知识点:");
        String know = sc.nextLine();
        Catalog catalog = new Catalog(id,know);
        ks.addCatalog(catalog);
        System.out.println("添加成功!");
    }

    public void start(String know) {
        while (true) {
        ArrayList<Knowledge> arrays = ks.findAllLogid(know);
        System.out.println("--------欢迎来到 <"+know+"> 知识管理系统--------");
        //这里是才是不需要,不对功能不一样,功能浏览全局,下面是浏览知识
        System.out.println("编号\t\t题目\t\t\t\t添加时间");
        for (int i = 0; i < arrays.size(); i++) {
            Knowledge kn = arrays.get(i);
            if (kn != null) {
                System.out.println(kn.getKnid()+ "\t\t" + kn.getSketch() +"\t\t" +kn.getInsert_time());
            }
        }
            System.out.println("请输入您的选择: 1.添加知识点  2.删除知识点  3.修改知识点  4.查看知识点  5.退出");
            if(know.equals("English")){
                System.out.println("6.汉译英");//7.英译汉

            }
            String choice = sc.nextLine();
            switch (choice) {
                case "1":
                    //添加
                    addKnowledge(know);
                    break;
                case "2":
                    //删除
                    deleteKnowledge(know);
                    break;
                case "3":
                    //修改
                   updataKnowledge(know);
                    break;
                case "4":
                    //查看
                    System.out.println("请输入需要查看的知识编号:");
                    String  id = sc.nextLine();
                    findAllKnowlege(know,id);
                    break;
                case "5":
                    //退出
                    System.out.println("感谢使用"+know+"知识管理系统,再见!");
                    return;
                case "6":
                    //汉译英
                    if(know.equals("English")){
                        Ctranslate("English");

                    }
                    break;
              /*  case "7":
                    //
                    if(know.equals("English")){

                    }
                    break;*/
                default:
                    System.out.println("您输入有误,请重新输入");
                    break;

            }
        }
    }

    private void Ctranslate(String English) {
        ArrayList<english> oldeng = ks.getNewenglish();
        System.out.println("请输入需要产生单词个数:");
        int c = sc.nextInt();
        sc.nextLine();
        ArrayList<english> eng = ks.Ctranslate("English",c);
        eng.addAll(oldeng);
        ArrayList<english> eng1 = new ArrayList<>();
        int conut = 0;
        for (int i = 0; i < eng.size(); i++) {
            english english = eng.get(i);
            System.out.println("第"+(i+1)+"题");
            System.out.println(english.getCword());
            String s = sc.nextLine();
            english.setNword(s);
        }

        for (int i = 0; i < eng.size(); i++) {
            english english = eng.get(i);
            if (english.getEword().equals(english.getNword())){
                conut ++;
            }else {
                System.out.println("正确单词:"+english.getEword()+"  错误单词:"+english.getNword());
                eng1.add(english);
            }
        }
        System.out.println("本次英语测试得分:"+conut);
        ks.newenglish(eng1);
        System.out.println("任意键退出");
        sc.nextLine();
    }

    private void updataKnowledge(String know) {
        System.out.println("请输入需要修改的知识编号:");
        String id = sc.nextLine();
        String textpart =null;
        String remark = null;
        int cho;
        if (ks.existsKnid(id,know)){
            System.out.println("请输入您要修改的标题:");
            String sketch = sc.nextLine();
           loop: while (true) {
                System.out.println("修改正文请输入:1,修改备注输入:2");
                cho = sc.nextInt();
                switch (cho){
                    case 1:
                        System.out.println("请输入您要修改的正文:");
                        textpart = ScannerSelf();
                        break loop;
                    case 2:
                        System.out.println("请输入您要修改的备注:");
                        remark = ScannerSelf();
                        break loop;
                        default:
                            System.out.println("输入有误,请重新输入");
                            break;

                }
            }


            // String time = null;
            /*ArrayList<Knowledge> array = kd.findAllLogid(know);
            for (int i = 0; i < array.size(); i++) {
                Knowledge knowledge1 = array.get(i);
                if (knowledge1 != null && knowledge1.getKnid().equals(id)) {
                    time = knowledge1.getTime();
                }
            }*/
            Date date =new Date();
            SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd");
            String time = simpleDateFormat.format(date);

            Knowledge knowledge = new Knowledge(id,sketch,textpart,remark,time);
            ks.updataKnowledge(id,knowledge,know,cho);
            System.out.println("修改成功!");
        }else {
            System.out.println("输入的编号有误!");
        }
    }

    public void deleteKnowledge(String know) {
        System.out.println("请输入需要删除的知识编号:");
        String id = sc.nextLine();
        ks.deleteKnowledge(id,know);

    }

    //需要进入精确查找
    public void findAllKnowlege(String know,String id) {
        ArrayList<Knowledge> arrays = ks.findAllLogid(know);
        boolean flag = false;
        int ii=0;
        for (int i = 0; i < arrays.size(); i++) {
            Knowledge kn = arrays.get(i);
            if (kn != null&&kn.getKnid().equals(id)) {
                //System.out.println(kn.getKnid()+ "\n" + kn.getSketch() +"\n"+kn.getTextpart()+"\n" + kn.getRemark()+"\n"+kn.getTime());
                System.out.print("编号为:");
                System.out.println(kn.getKnid());
                System.out.print("标题:");
                System.out.println(kn.getSketch());
                System.out.print("正文:");
                System.out.println(kn.getTextpart());
                System.out.print("备注:");
                System.out.println(kn.getRemark());
                System.out.print("建立时间:");
                System.out.println(kn.getInsert_time());
                flag = true;
                ii=i;
                break;
            }
        }
        if (!flag){
            System.out.println("输入的编号有误!");
        }
        while (true) {
            System.out.println("e.退出 按回车键进入浏览模式");
            String var = sc.nextLine();
            if (var.equals("e")){
                return;
            }else {
                findAllKnowlege1(know,ii);
            }
        }

    }

    public void findAllKnowlege1(String know,int id) {
        ArrayList<Knowledge> arrays = ks.findAllLogid(know);
        Knowledge kn = arrays.get(id);
        System.out.print("编号为:");
        System.out.println(kn.getKnid());
        System.out.print("标题:");
        System.out.println(kn.getSketch());
        System.out.print("正文:");
        System.out.println(kn.getTextpart());
        System.out.print("备注:");
        System.out.println(kn.getRemark());
        System.out.print("建立时间:");
        System.out.println(kn.getInsert_time());
            System.out.println("e.退出 [.后退  ].前进");
            String var = sc.nextLine();
            if (var.equals("e")) {
                return;
            } else if(var.equals("]")){
                if (id>=arrays.size()-1){
                    id = 0;
                }else {
                    id++;
                }
            }else if(var.equals("[")){
                if (id<=0){
                    id=arrays.size()-1;
                }else {
                    id--;
                }
            }else {
                System.out.println("输入有误!");
                return;
            }
        findAllKnowlege1(know,id);
    }


    public void addKnowledge(String know) {
        ArrayList<Knowledge> numberk = ks.findAllLogid(know);
        String knid1 = numberk.get(numberk.size() - 1).getKnid();
        //System.out.println(knid1);
        int length = knid1.length();
        String substring = knid1.substring(length - 3, length);
        //System.out.println(substring);
        int i = Integer.parseInt(substring);
        //System.out.println(i);
        i++;
        String ii = i+"";
        if (ii.length()==1){
            ii="00"+ii;
        }else if(ii.length()==2){
            ii="0"+ii;
        }
        String id = know.substring(0,2)+ii;
       // System.out.println(id);
        String remark ="";
        System.out.println("当前知识点编号ID为"+id);

       /* System.out.println("请输入您要添加的"+know+"知识编号:(格式为:"+know+"_00x)");
        while (true) {
            id = sc.nextLine();
            boolean exists = ks.existsKnid(id,know);
            if (!exists) {
                break;
            } else {
                System.out.println("你输入的"+know+"知识编号已经存在,请重新输入:");
            }
        }*/
        System.out.println("请输入该知识点的题目:");
        String sketch = sc.nextLine();
        System.out.println("请输入具体描述:");
        String textpart = ScannerSelf();
        System.out.println("回车跳过备注 b.输入备注");
        String s1 = sc.nextLine();
        if (s1.equals("b")) {
            System.out.println("请输入备注:");
             remark = ScannerSelf();
        }
        Date date =new Date();
        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd");
        String time = simpleDateFormat.format(date);
        //System.out.println(time);
        Knowledge knd = new Knowledge(id,sketch,textpart,remark,time);
        ks.addKnowledge(knd,know);
        System.out.println("添加成功!");
        System.out.println("回车继续添加 q.退出");
        String s = sc.nextLine();
        if (s.equals("q")){
            return;
        }else {
        addKnowledge(know);}
    }
    public String ScannerSelf(){
        String flag = "";
        String sss = "";
        String ss = "";
        do{
            sss=sss+ss+'\n';
            ss = sc.nextLine();
        }while (!ss.equals(":w"));
        return sss;
    }

    public void exercise() {
        System.out.println("欢迎来到知识练习题库,请输入今天需要题量:(根据题量每个知识点随机产生,请输入1-10)");
        //String ch = sc.nextLine();
        //int chi = Integer.parseInt(ch);
        ArrayList<NewTable> array;
        while (true) {
            int ch = sc.nextInt();
            if (ch>0&&ch<99){
             array = ks.exercise(ch);
            break;
            }else{
                System.out.println("输入有误!请重新输入:");
            }
        }
        ArrayList<NewTable> array1 =ks.getNewTable();
        array.addAll(array1);
       // Iterator<NewTable> it = array.iterator();
       // while (it.hasNext()) {
        //int j=0;
        //int x=array.size();
        for (int i = 0; i <array.size() /*x+j*/; i++) {
            //int i =1;
            System.out.println("第" + (i+1) + "题:");
            NewTable newTable1 = array.get(i); //it.next();
            System.out.println(newTable1.getNewid() + ":");
            System.out.println();
            System.out.println(newTable1.getNewsk() + ":");
            System.out.println();
            System.out.println("答题:");
            String s = ScannerSelf();
            newTable1.setAsk(s);
            System.out.println("1.继续,2.帮助");
            int cho2 = sc.nextInt();
            if (cho2 == 1) {
                continue;
            } else if (cho2 == 2) {
                System.out.println("正文:");
                System.out.println(newTable1.getNewtext());
                NewTable newTable2 = new NewTable(newTable1.getNewid(),newTable1.getNewsk(),
                        newTable1.getNewtext(),newTable1.getNewre(),newTable1.getAsk());
                array.add(newTable2);
                newTable1.setNewre("0");
                //j++;
                sc.nextLine();
                System.out.println("c.继续");
                String cho3 = sc.nextLine();
                if (cho3.equals("c")) {
                    continue;
                }
            } else {
                System.out.println("输入有误!");
            }
        }
        System.out.println("-----------开始评分----------------");
        sc.nextLine();
        Iterator<NewTable> it = array.iterator();
        while (it.hasNext()){
            NewTable newTable = it.next();
            if (newTable.getNewre()!=null&&!newTable.getNewre().equals("0")){
                System.out.println("编号");
                System.out.println(newTable.getNewid());
                System.out.println("题目");
                System.out.println(newTable.getNewsk());
                System.out.println("答案");
                System.out.println(newTable.getNewtext());
                System.out.println("作答");
                System.out.println(newTable.getAsk());
                System.out.println("p.通过 f.未通过");
                String c = sc.nextLine();
                if (c.equals("p")){
                    it.remove();
                }
            }else if(newTable.getNewre()!=null&&newTable.getNewre().equals("0")){
                it.remove();
            }
        }
         ks.newtable(array);


    }

}



